﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using eschoolBO;

namespace ESCHOOL.Client
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        student stud = new student();
        teacher teach = new teacher();
        parent par = new parent();
        protected void Page_Load(object sender, EventArgs e)
        {
            Label2.Visible = false;
            if (!IsPostBack)
            {
                if (Request["id"] != null)
                {
                    Session.Abandon();
                    Response.Redirect("~/Home.aspx");
                }
                lbsignin.Visible = false;
                Panel2.Visible = false; Label1.Visible = false;
                //Calendar1.SelectedDate = DateTime.Today;
            }
        }



        protected void btnlogin_Click(object sender, EventArgs e)
        {
            if (RadioButtonList1.SelectedValue == "1")
            {

                DataTable dt = stud.GetRecords(-99, "", -99, -99, "", "", "", "", "", "", -99, -99, "", -99, tbusername.Text, tbpassword.Text, "","",false, " and ");
                if (dt.Rows.Count > 0)
                {
                    Session["userid"] = dt.Rows[0]["student_id"].ToString();
                    //Session["password"] = dt.Rows[0]["password"].ToString();
                    Response.Redirect("~/students/home.aspx");
                }

            }
            
            if (RadioButtonList1.SelectedValue == "2")
            {
                DataTable dt1 = teach.GetRecords(-99, "", "", "", "", "", "", -99, -99, -99, tbusername.Text,tbpassword.Text,"","","", false, " and ");
                if (dt1.Rows.Count>0)
                {
                    Session["userid"] = dt1.Rows[0]["teacher_id"].ToString();
                    //Session["password"] = dt1.Rows[0]["password"].ToString();
                        Response.Redirect("~/teachers/home.aspx");
                }
            }
            
            if (RadioButtonList1.SelectedValue == "3")
            {
                DataTable dt2 = par.GetRecords(-99, "", "", "", "", "", "", "", -99, "", -99, tbusername.Text,tbpassword.Text, -99,"","", false, " and ");
                if (dt2.Rows.Count>0)
                {
                    Session["userid"] = dt2.Rows[0]["parent_id"].ToString();
                    //Session["password"] = dt2.Rows[0]["password"].ToString();
                        Response.Redirect("~/parents/home.aspx");
                }
            }
            Label2.Visible = true;
            Label2.Text = "Login failed! try again.";
        }

        protected void btncancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/client/home.aspx");
        }

        protected void lbforgetpss_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false; Panel2.Visible = true;
        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {
            if (RadioButtonList2.SelectedValue == "1")
            {

                DataTable dt = stud.GetRecords(-99, "", -99, -99, "", "", "", "", "", "", -99, -99, "", -99, "","",ddquestion.SelectedValue.ToString(),tbanswer.Text, false, " and ");
                if (dt.Rows.Count > 0)
                {
                    Label1.Text = "Your password is " + dt.Rows[0]["password"].ToString(); Label1.Visible = true;
                    Panel2.Visible = false; lbsignin.Visible = true;
                }
                else
                { Label1.Text = "You are not a valid user"; Label1.Visible = true; }
            }

            if (RadioButtonList2.SelectedValue == "2")
            {
                DataTable dt1 = teach.GetRecords(-99, "", "", "", "", "", "", -99, -99, -99, "", "", "", ddquestion.SelectedValue.ToString(), tbanswer.Text, false, " and ");
                if (dt1.Rows.Count > 0)
                {
                    Label1.Text = "Your password is " + dt1.Rows[0]["password"].ToString(); Label1.Visible = true;
                    Panel2.Visible = false; lbsignin.Visible = true;
                }
                else
                { Label1.Text = "You are not a valid user"; Label1.Visible = true; }
            }

            if (RadioButtonList2.SelectedValue == "3")
            {
                DataTable dt2 = par.GetRecords(-99, "", "", "", "", "", "", "", -99, "", -99, "", "", -99, ddquestion.SelectedValue.ToString(), tbanswer.Text, false, " and ");
                if (dt2.Rows.Count > 0)
                {
                    Label1.Text = "Your password is " + dt2.Rows[0]["password"].ToString(); Label1.Visible = true;
                    Panel2.Visible = false; lbsignin.Visible = true;
                }
                else
                { Label1.Text = "You are not a valid user"; Label1.Visible = true; }

            }
            
        }

        protected void lbsignin_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true; lbsignin.Visible = false; Label1.Visible = false;
        }

        

        //protected void rbstudent_CheckedChanged(object sender, EventArgs e)
        //{

        //}

        //protected void rbteacher_CheckedChanged(object sender, EventArgs e)
        //{

        //}

        //protected void rbparent_CheckedChanged(object sender, EventArgs e)
        //{

        //}
    }
}
